<!-- 员工编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="staffModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="staffModalApp.staff"
        :rules="staffModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="姓名:" name="name">
              <a-input
                v-model:value="staffModalApp.staff.name"
                placeholder="请输入姓名"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="联系电话:" name="contactTel">
              <a-input
                v-model:value="staffModalApp.staff.contactTel"
                placeholder="请输入联系电话"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="电子邮箱:" name="email">
              <a-input
                v-model:value="staffModalApp.staff.email"
                placeholder="请输入电子邮箱"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="状态:" name="status">
              <a-input
                v-model:value="staffModalApp.staff.status"
                placeholder="请输入状态"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="职务:" name="post">
              <m-dict-select
                v-model:value="staffModalApp.staff.post"
                placeholder="请选择角色"
                dict="员工角色"/>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="staffModalApp.display">
                <teleport to="#staffEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="staffListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(staffModalApp.staff)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!staffModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(staffModalApp.staff)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted,ref} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {StaffService} from "@/views/restaurant/staff/staff/staffService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';
import MDictSelect from "@/components/MDict/dictSelect";


export default defineComponent({
  components: {MDictSelect},
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let staffListApp = inject('staffListApp', reactive({}));
    let staffGrid = inject('staffGrid',ref())
    const staffModalApp = reactive({
      id: null,
      staff: {},
      isEdit: false,
      display: false
    });
    staffModalApp.rules = {
      contactTel: [
        {
          required: true,
          type: 'string',
          message: '请输入联系电话',
          trigger: 'blur'
        }
      ],
      email: [
        {
          required: false,
          type: 'string',
          message: '请输入电子邮箱',
          trigger: 'blur'
        }
      ],
      post: [
        {
          required: true,
          type: 'string',
          message: '请输入职务',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      staffModalApp.staff = reactive({});
      if (routeId) {//路由
        staffModalApp.id = routeId;
        staffModalApp.display = false;
        findStaff(staffModalApp.id);
      } else if (staffListApp.editModalForEdit) {//修改弹窗
        staffModalApp.id = staffListApp.currentId;
        staffModalApp.display = true;
      } else if (!staffListApp.editModalForEdit && staffListApp.addModalForEdit) {//新增弹窗
        staffModalApp.display = true;
      } else {
        staffModalApp.title = "新增学生";
      }
      if (staffModalApp.id) findStaff(staffModalApp.id);
    })

    const findStaff = (id) => {
      StaffService.findStaffForView(id).then((res) => {
        staffModalApp.staff = res.data;
        //TODO:为编辑准备.
        staffModalApp.title = "编辑学生 " + staffModalApp.staff.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找学生出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        StaffService.updateStaff(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        StaffService.saveStaff(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/restaurant/staff/staff`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        staffGrid.value.commitProxy("query");
      }, 1500);
      staffListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      staffListApp,
      staffModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
